home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 03 / findfile.bas < prev    next >
BASIC Source File  |  1990-08-17  |  4KB  |  71 lines

  1. '***********************************************************************
  2. '                             FINDFILE.BAS                             *
  3. '  Two functions to be linked to programs compiled with BC version 7   *
  4. '----------------------------------------------------------------------*
  5. '    MAKNAME(<filespec>) [DECLARE FUNCTION MAKNAME$ (A$)] returns the  *
  6. ' <filename.ext> portion of <filespec>, where <filespec> can be either *
  7. ' a string variable or string literal.                                 *
  8. '----------------------------------------------------------------------*
  9. '    FINDFILE(<filespec>) [DECLARE FUNCTION FINDFILE$ (A$)] locates a  *
  10. ' file stored in the specified directory or in a directory on the PATH *
  11. ' environment string, using the "new" BASIC function DIR$ to test for  *
  12. ' existence of a file.  FINDFILE returns a string containing the full  *
  13. ' <filespec> (including the drive and directory path as required).  If *
  14. ' the input argument contains "wildcards," FINDFILE will return the    *
  15. ' expanded <filespec> of the first file found that meets the input     *
  16. ' specification.  If the requested file cannot be found after the      *
  17. ' PATH environment string search, FINDFILE returns a null string.      *
  18. '----------------------------------------------------------------------*
  19. '               Rewritten by M. L. Lesser, April 8, 1990               *
  20. ' Based on the BC version 4.0 subroutines in the module of the same    *
  21. ' name, written for the book "Advanced QuickBASIC 4.0," published by   *
  22. ' Bantam Books, 1988.                                                  *
  23. '                Compiled with Microsoft BC version 7.1                *
  24. '***********************************************************************
  25.  
  26.     DEFINT I-K
  27.     DEFSTR F,M,P,T
  28.         
  29. FUNCTION MAKNAME(FILENAME) STATIC
  30.     LET J = INSTR(FILENAME,"\")         'Separator in <filespec> "path"
  31.     LET K = 0                           'Place mark
  32.     WHILE J <> 0
  33.         LET K = J                           'K will be location of
  34.         LET J = INSTR(K+1,FILENAME,"\")     ' last "\" in FILENAME
  35.     WEND
  36.     IF K = 0 THEN LET K = INSTR(FILENAME,":")  'If no path, try drive
  37.     IF K <> 0 THEN
  38.         LET MAKNAME = MID$(FILENAME,K+1)
  39.     ELSE
  40.         LET MAKNAME = FILENAME
  41.     END IF
  42. END FUNCTION
  43.  
  44. FUNCTION FINDFILE(FILE) STATIC
  45.     LET PATH = ENVIRON$("PATH") 'Get PATH environment string
  46. ' Capture any FPATH from input FILE in case hit on first directory:
  47.     LET FPATH = UCASE$(LEFT$(FILE,INSTR(FILE,MAKNAME(FILE))-1))
  48.     LET TEMP = FILE                     'Local variable
  49. ' Now find the file:
  50.     WHILE LEN(DIR$(TEMP)) = 0           'As long as file not found
  51.         LET I = INSTR(PATH,";")             'Separator in PATH string
  52.         IF PATH = "" THEN                   'If no more directories
  53.            LET FINDFILE = ""                    'Error signal and forced
  54.            EXIT FUNCTION                        '  return from function
  55.         END IF
  56.         IF I <> 0 THEN                      'If more than one "path"
  57.            LET FPATH = LEFT$(PATH,I-1)          'Try next one and
  58.            LET PATH = MID$(PATH,I+1)            ' strip it off string
  59.         ELSE                                'This is last one on string
  60.            LET FPATH = PATH                     'So use it
  61.            LET PATH = ""                        '  and set PATH at null
  62.         END IF
  63.         IF RIGHT$(FPATH,1) <> "\" THEN      'If not a root directory
  64.            LET FPATH = FPATH + "\"              '  add trailing "\" 
  65.         END IF
  66.         LET TEMP = MAKNAME(TEMP)            'Delete old "path,"
  67.         LET TEMP = FPATH + TEMP             '  insert new value,
  68.     WEND                                  '  and try again
  69.     LET FINDFILE = FPATH + DIR$(TEMP)
  70. END FUNCTION
  71.